<?php

require_once('includes/pdoconnect.php');
require_once('includes/pgpdoconnect.php');
require_once('includes/dataholderclasses.php');
require_once('includes/biocaseunit.php');
require_once('includes/pgbiocaseload.php');

$argv[1] = 'daily';
if (!isset($argv[1])) {
    exit ("Argument missing...\n");
}

    $starttime = getStartTime($argv);
    $update = new UpdateBioCase($db, $pgdb, $starttime);
    $update->updateUnits();

function getStartTime($argv) {
    date_default_timezone_set('Australia/Melbourne');
    if($argv[1] == 'reindex')
        $starttime = FALSE;
    elseif(eregi('^[0-9]{4}-[0-9]{2}-[0-9]{2}$', $argv[1])) $starttime = $argv[1];
    else {
        $date = new DateTime(date('Y-m-d H:i:s'));
        switch ($argv[1]) {
            case 'hourly':
                $date->sub(new DateInterval('PT01H'));
                break;

            case 'daily':
                $date->sub(new DateInterval('P01D'));
                break;

            case 'weekly':
                $date->sub(new DateInterval('P07D'));
                break;
            
            case 'monthly':
                $date->sub(new DateInterval('P01M'));
                break;

            default:
                break;
        }
        $starttime = $date->format('Y-m-d H:i:s');
    }
    return $starttime;
}

class UpdateBioCase {
    var $db;
    private $pgdb;
    private $starttime;
    private $reindex;
    
    public function __construct($db, $pgdb, $starttime) {
        $this->db = $db;
        $this->pgdb = $pgdb;
        $this->starttime = $starttime;
        $this->reindex = ($this->starttime) ? FALSE : TRUE;
    }

    function updateUnits() {
        $select = "SELECT CollectionObjectID 
            FROM collectionobject";
        
        if ($this->starttime) {
            $select .= " WHERE TimestampModified>='$this->starttime'";
        }
        $reindex = ($this->starttime) ? FALSE : TRUE;

        $stmt = $this->db->prepare($select);
        $stmt->execute();

        while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
            $update = new BioCASeUnit($this->db, $row->CollectionObjectID);
            $load = new BioCASeLoad($this->pgdb, $row->CollectionObjectID);

            $update->updateUnit();
            $load->load('unit', $update->Unit, $this->reindex);

            $update->updateIdentification();
            $load->load('identification', $update->Identifications, $this->reindex);
            $load->load('abcd_highertaxon', $update->HigherTaxa, $this->reindex);

            $update->updateCollector();
            $load->load('collector', $update->Collectors, $this->reindex);

            $update->updateNamedArea();
            $load->load('namedarea', $update->NamedAreas, $this->reindex);

            $update->updatePreviousUnits();
            $load->load('previousunit', $update->PreviousUnits, $this->reindex);

            $update->updateUnitMeasurementOrFact();
            $load->load('unitmeasurementorfact', $update->UnitMeasurementOrFacts, $this->reindex);
        }
    }


}



?>
